﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CuteSprite.Eme.Data.Settlement;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Components;
using HmFramework.Components.Controls;
using HmFramework.Components.Editors;
using HmFramework.Data;
using HmFramework.UI.Utils.Controls;
using CuteSprite.Eme.Data;

namespace CuteSprite.Eme.UI.Settlement
{
	public partial class FrmBillingSolution : EmeEntityForm
	{
		#region -- 属性 --

		private Int32 _SettlementSolutionID;

		/// <summary>公布价方案ID</summary>
		public Int32 SettlementSolutionID
		{
			get { return _SettlementSolutionID; }
			set { _SettlementSolutionID = value; }
		}

		private BillingSolution _CurrentBilling;

		/// <summary>当前计费方案实体对象</summary>
		public BillingSolution CurrentBilling
		{
			get
			{
				if (_CurrentBilling != null) { return _CurrentBilling; }
				_CurrentBilling = CurrentEntity as BillingSolution;
				return _CurrentBilling;
			}
			set { _CurrentBilling = value; }
		}

		private String _SettlementSolutionCode;

		/// <summary>  公布价方案代码 </summary>
		public String SettlementSolutionCode
		{
			get { return _SettlementSolutionCode; }
			set { _SettlementSolutionCode = value; }
		}

		#endregion

		#region -- 构造 --

		#region - 新增 -

		/// <summary>默认构造函数</summary>
		public FrmBillingSolution(Int32 settlementSolutionID, String settlementSolutionCode, IEntity entity)
			: base(entity, false)
		{
			SettlementSolutionID = settlementSolutionID;
			SettlementSolutionCode = settlementSolutionCode;

			InitializeComponent();
			InitForm();
		}

		#endregion

		#region - 编辑或新增 -

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		public FrmBillingSolution(String settlementSolutionCode, IEntity entity)
			: base(entity, false)
		{
			SettlementSolutionCode = settlementSolutionCode;

			InitializeComponent();
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmBillingSolution(IEntity entity, Boolean readOnly)
			: base(entity, readOnly)
		{
			InitializeComponent();
			InitForm();
		}

		#endregion

		#endregion

		#region -- 窗体事件 --

		#region - method InitComponent -

		///// <summary>控件初始化</summary>
		protected override void InitComponent()
		{
			base.InitComponent();

			if (frmBusinessCategory == null) { frmBusinessCategory = FindControl("frmBusinessCategory") as ComboBoxEx; }
			if (frmBusinessCategory != null)
			{
				frmBusinessCategory.SelectedIndexChanged += new EventHandler((sender, e) =>
				{
					ExpressBusinessCategory SelectedValue = (ExpressBusinessCategory)frmBusinessCategory.SelectedValue;
					if (SelectedValue == ExpressBusinessCategory.ExpressOCS)
					{
						EntityList<Country> country = null;

						// 获取地区实体工厂
						var ssFactory = EmeEntityFactory.Create(EntityHelper.ModelUserCenter, EntityHelper.UserCenterTableCountry);
						var cache = EmeRemotingHelper.FindEntityList(EntityHelper.ModelUserCenter, EntityHelper.UserCenterTableCountry);
						if (cache != null) { country = cache.FindAll() as EntityList<Country>; }
						frmRegion.DataSource = null;
						frmRegion.ValueMember = Country.__.CountryCode2;
						frmRegion.DisplayMember = Country.__.CountryCode2;
						frmRegion.DataSource = country;
					}
					else
					{
						frmRegion.DataSource = null;
					}
				});

			}

			if (frmGoodsCategory == null) { frmGoodsCategory = FindControl("frmGoodsCategory") as ComboBoxEx; }

			this.OnSaving += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmSettlementSolution_OnSaving);
			this.OnValid += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmBsdSettlementSolution_OnValid);
		}

		#endregion

		#region - method FormOnPreLoad -

		protected override void FormOnPreLoad()
		{
			base.FormOnPreLoad();

			if (IsAddNew)
			{
				CurrentBilling.SettlementSolutionID = SettlementSolutionID;
			}
		}

		#endregion

		#region - method FormOnLoad -

		public override void FormOnLoad()
		{
			base.FormOnLoad();

			if (!IsAddNew)
			{
				ExpressBusinessCategory BusinessCategory = (ExpressBusinessCategory)CurrentEntity[BillingSolution._.BusinessCategory];
				if (BusinessCategory == ExpressBusinessCategory.ExpressOCS)
				{
					EntityList<Country> country = null;

					// 获取地区实体工厂
					var ssFactory = EmeEntityFactory.Create(EntityHelper.ModelUserCenter, EntityHelper.UserCenterTableCountry);
					var cache = EmeRemotingHelper.FindEntityList(
							EntityHelper.ModelUserCenter, EntityHelper.UserCenterTableCountry,
							null, null, null, 0, 0, false, null, false);
					if (cache != null)
					{
						country = cache.FindAll() as EntityList<Country>;
					}
					frmRegion.DataSource = null;
					frmRegion.ValueMember = Country.__.CountryCode2;
					frmRegion.DisplayMember = Country.__.CountryCode2;
					frmRegion.DataSource = country;
					frmRegion.SelectedValue = CurrentEntity[BillingSolution.__.Region];
				}
				else
				{
					frmRegion.DataSource = null;
				}
			}
		}

		#endregion

		#region - 实体保存 -

		private void FrmSettlementSolution_OnSaving(object sender, EntityFormEventArgs e)
		{

		}

		#endregion

		#region - 实体验证 -

		private void FrmBsdSettlementSolution_OnValid(object sender, EntityFormEventArgs e)
		{
			if (frmBusinessCategory != null && (Int32)frmBusinessCategory.SelectedValue <= 0)
			{
				MessageBoxEx.Show("请选择业务类别！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				e.Cancel = true;
				return;
			}
			if (frmGoodsCategory != null && (Int32)frmGoodsCategory.SelectedValue <= 0)
			{
				MessageBoxEx.Show("请选择货物类别！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				e.Cancel = true;
				return;
			}
			if (frmRegion.DataSource == null)
			{
				MessageBoxEx.Show("请选择地区！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				e.Cancel = true;
				return;
			}
			if (frmSettlementSolutionID.SelectTarget == null && frmSettlementSolutionID.SelectedID == 0)
			{
				MessageBoxEx.Show("请选择公布价方案！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				e.Cancel = true;
				return;
			}
			if (frmAdditionalChargesID.SelectTarget == null && frmAdditionalChargesID.SelectedID == 0)
			{
				MessageBoxEx.Show("请选择附加费用！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				e.Cancel = true;
				return;
			}
			if (frmBillingPriceID.SelectTarget == null && frmBillingPriceID.SelectedID == 0)
			{
				MessageBoxEx.Show("请选择计费价格！", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				e.Cancel = true;
				return;
			}
		}

		#endregion


		#endregion
	}
}